package com.shigen.demo.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Ticket02 { //资源类
    private int number = 30;

    // 可重入锁
    private Lock lock = new ReentrantLock();

    public  void saleTicket() {
        lock.lock();
        if (number > 0) {
            System.out.println(Thread.currentThread().getName() + "卖出第" + number-- + "张票，还剩" + number);
        }
        lock.unlock();
    }
}

public class demo02 {
    public static void main(String[] args) {
        Ticket02 ticket = new Ticket02();
        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.saleTicket();
            }
        }, "A").start();
        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.saleTicket();
            }
        }, "B").start();
        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.saleTicket();
            }
        }, "C").start();
    }
}
